package persistentie;

import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Robin
 */
public class DbConnection {

    private final String url = "jdbc:mysql://nt2/SOIIIGroepJ4";
    private final String username = "SOIIIGroepJ4";
    private final String password = "SOIIIGroepJ4";
    private final String driver = "com.mysql.jdbc.Driver";
    private static Connection conn = null;
    private static DbConnection instance;

    public DbConnection() {
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Geen verbinding" + "\n" + url, "",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }

    public synchronized static DbConnection getInstance() {
        if (instance == null) {
            instance = new DbConnection();
        }
        return instance;
    }

    public Connection getConnection() {
        return conn;
    }

    public ResultSet getResultSet(String sql) {
        ResultSet rs = null;
        try {
            Statement s = conn.createStatement();
            rs = s.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }

    public void executeSQL(String sql) {
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
